package com.hankai.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import com.hankai.common.ServerResponse;
import com.hankai.dao.CategoryMapper;
import com.hankai.pojo.Category;
import com.hankai.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * Created by hankai on 2017/6/7.
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    private static Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if(parentId==null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByError("参数有误");
        }
        Category category = new Category();
        category.setParentId(parentId);
        category.setName(categoryName);
        category.setStatus(true);
        int result = categoryMapper.insert(category);
        if(result>0){
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createByError("添加失败");
    }

    @Override
    public ServerResponse updateCategory(String categoryName, Integer categoryId) {
        if(categoryId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByError("参数有误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int result = categoryMapper.updateByPrimaryKeySelective(category);
        if(result>0){
            return ServerResponse.createByError("修改成功");
        }
        return ServerResponse.createBySuccess("修改失败");
    }

    @Override
    public ServerResponse getChildenParallelCategory(Integer parentId) {
        List<Category> categoryList = categoryMapper.selectChildenCategory(parentId);
        if(CollectionUtils.isEmpty(categoryList)){
            logger.error("没有子节点");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    @Override
    public ServerResponse getChildenAndDeepParallelCategory(Integer categoryId) {
        Set<Category> categorySet = Sets.newHashSet();
        categorySet = findChildenCategory(categorySet,categoryId);
        List<Integer> categoryIdList = Lists.newArrayList();
        if(categoryId!=null){
            for(Category category:categorySet){
                categoryIdList.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }

    /**
     * 循环查出子节点
     * @param id
     * @return
     */
    public Set<Category> findChildenCategory(Set<Category> categorySet,Integer id){
       Category category = categoryMapper.selectByPrimaryKey(id);
        if(category!=null){
            categorySet.add(category);
        }
        List<Category> categories = categoryMapper.selectChildenCategory(id);
        for(Category category1:categories){

            findChildenCategory(categorySet,category1.getId());
        }


        return categorySet;
    }
}
